
var netArea = "";
var staffNo = "";
var isOperating = false;

function load(){
	var message = $('#message').val().trim();
	if (message != "" && message != null) {
		alert(message);
	}
	search();
	loadProvinces();
}

//编辑店铺基础数据
function editSiteData(element) {
	if(!isOperating){
		isOperating=true;

		
		var parentDiv = $(element).parent();
		var parentTd = $(parentDiv).parent();
		var parentTr = $(parentTd).parent();
		var arrTd = $(parentTr).children();
		var siteKeyTd = arrTd[0];
		var siteId = $(siteKeyTd).text().trim();
		
		var netAreaTd = arrTd[2];
		var staffNoTd = arrTd[3];
		
		$(parentDiv).children("a").hide();
		var cancelHtml = "<a onclick="+"\""+"cancel(this);"+"\""+
			"class='detail fontBlue' href='#'><span>取消</span> </a>";
		$(parentDiv).append(cancelHtml);
		var confirmHtml = "<a onclick="+"\""+"updateSiteData(this);"+"\""+
		"class='detail fontBlue' href='#'><span>确定</span></a>";
		$(parentDiv).append(confirmHtml);
		
		var txtN = $(netAreaTd).text().trim();
		netArea = txtN;
		$(netAreaTd).text("");
		var inputHtml = "<input type='text' style='width:50px' id='txtNetArea' value='"+ txtN +"' />"
		$(netAreaTd).append(inputHtml);
		
		var txtS = $(staffNoTd).text().trim();
		staffNo = txtS;
		$(staffNoTd).text("");
		var inputHtml = "<input type='text' style='width:50px' id='txtStaffNo' value='"+ txtS +"' />"
		$(staffNoTd).append(inputHtml);
	}
}

function cancel(element){
	
	var parentDiv = $(element).parent();
	var parentTd = $(parentDiv).parent();
	var parentTr = $(parentTd).parent();
	var arrTd = $(parentTr).children();
	var siteKeyTd = arrTd[0];
	var siteId = $(siteKeyTd).text().trim();
	
	var netAreaTd = arrTd[2];
	var staffNoTd = arrTd[3];

	$(parentDiv).children("a").remove();
	var editHtml = "<a onclick="+"\""+"editSiteData(this);"+"\""+
	"class='detail fontOrange' href='#'><span>编辑</span> </a>";
	$(parentDiv).append(editHtml);
	var detailHtml = "<a onmouseover="+"\""+"querySiteData(this);"+"\""+
	"class='detail fontOrange' href='#'><span>详情</span> </a>";
	$(parentDiv).append(detailHtml);
	
	$(netAreaTd).children("input").remove();
	$(netAreaTd).text(netArea);
	
	$(staffNoTd).children("input").remove();
	$(staffNoTd).text(staffNo);

	if(isOperating){
		isOperating=false;
	}
}

// 点击确定，更新数据
function updateSiteData(element){

	var area = 0;
	var staffNum = 0;
	var flag = true;
	
	var parentDiv = $(element).parent();
	var parentTd = $(parentDiv).parent();
	var parentTr = $(parentTd).parent();
	var arrTd = $(parentTr).children();
	var siteKeyTd = arrTd[0];
	var siteId = $(siteKeyTd).text().trim();
	
	var netAreaTd = arrTd[2];
	var staffNoTd = arrTd[3];
	
	var txtN = $(netAreaTd).children("input").attr("value").trim();
	txtN = txtN == undefined ? netArea : txtN;
	
	var txtS = $(staffNoTd).children("input").attr("value").trim();
	txtS = txtS == undefined ? staffNum : txtS;
	
	if(!(isDecimal(txtN)) || !isNumber(txtS)){
		flag = false;
		alert("请输入正确的数据");
		return;
	}
	
	area = txtN;
	$(netAreaTd).children("input").remove();
	$(netAreaTd).text(txtN);
	
	staffNum = txtS;
	$(staffNoTd).children("input").remove();
	$(staffNoTd).text(txtS);
	
	if(flag){
		$(parentDiv).children("a").remove();
		var editHtml = "<a onclick="+"\""+"editSiteData(this);"+"\""+
		"class='btnsub fontOrange' href='#'><span>编辑</span> </a>";
		$(parentDiv).append(editHtml);
		var detailHtml = "<a onmouseover="+"\""+"querySiteData(this);"+"\""+
		"class='btnsub1 fontOrange' href='#'><span>详情</span> </a>";
		$(parentDiv).append(detailHtml);
		
		$.ajax({
			url:"updateSiteData",
			type:"post",
			dataType:"json",
			async:false,
	        data: { //发送给数据库的数据
			 siteId: siteId,
			 netArea: area,
			 staffNum: staffNum
			 },
			success:function(msg){
			},
			error: function (XMLHttpRequest, textStatus, thrownError) {
			}
		
		});
	}
	if(isOperating){
		isOperating=false;
	}
}

function querySiteData(element){
	var parentDiv = $(element).parent();
	var parentTd = $(parentDiv).parent();
	var parentTr = $(parentTd).parent();
	var arrTd = $(parentTr).children();
	var siteKeyTd = arrTd[0];
	var siteId = $(siteKeyTd).text().trim();

    $(element).qtip({
        content: {
            // 设置您要使用的文字图像的HTML字符串，正确的src URL加载图像
            text: '<img class="throbber" src="loading.gif" alt="Loading..." />',
            url: 'queryBySiteId?siteId='+siteId, // 使用的URL加载的每个元素的rel属性
            prerender: true,
            title: {
                text: '店铺信息', // 给工具提示使用每个元素的文本标题
                button: '关闭' // 在标题中显示关闭文字按钮
            }
        },
        position: {
            corner: {
                target: 'bottomMiddle', // 定位上面的链接工具提示
                tooltip: 'topMiddle'
            },
            adjust: {
                screen: true // 在任何时候都保持提示屏幕上的
            }
        },
        show: {
            when: 'mouseover', // 或click
            solo: true // 一次只显示一个工具提示
        },
        hide: 'mouseout',
        style: {
            tip: true, // 设置一个语音气泡提示在指定工具提示角落的工具提示
            border: {
                width: 0,
                radius: 4
            },
            name: 'light', // 使用默认的淡样式
            width: 390 // 设置提示的宽度
        }
    })
;

}


function loadProvinces(choosedProvinceId,choosedCityId,choosedAreaId){
	
    $("#provice option:gt(0)").remove();
    $("#city option:gt(0)").remove();
    $("#area option:gt(0)").remove();
    
	$.ajax({
		url:"getProvinces",
		type:"get",
		dataType:"json",
		async:false,
		success:function(msg){
			for (var i=0; i<msg.child_region.length; i++){
				if(choosedProvinceId == msg.child_region[i].regionId){
					$("#province").append('<option value="' + msg.child_region[i].regionId + '"' +'selected="true"'+  ' >' +msg.child_region[i].regionName + '</option>');
					continue;
				}
				$("#province").append('<option value="' + msg.child_region[i].regionId + '">' + msg.child_region[i].regionName + '</option>');
			}
		},
		error: function (XMLHttpRequest, textStatus, thrownError) {
			
		}
	
	})
	
	loadCitys(choosedCityId,choosedAreaId);
	loadSquares();
	
}

function loadCitys(choosedCityId,choosedAreaId){


	$("#city option:gt(0)").remove();
	$("#area option:gt(0)").remove();
	var provinceId = $("#province").val().trim()
	if ($.trim(provinceId) > 0)
	{
		$.ajax({
		url:"getCitys",
		type:"get",
		dataType:"json",
		async:false,
        data: { //发送给数据库的数据
            provinceId:provinceId
            },
		success:function(msg){
				for (var i=0; i<msg.child_region.length; i++){
					if(choosedCityId == msg.child_region[i].regionId){
						$("#city").append('<option value="' + msg.child_region[i].regionId + '"' +'selected="true"'+  ' >' +msg.child_region[i].regionName + '</option>');
						continue;
					}
					$("#city").append('<option value="' + msg.child_region[i].regionId + '">' + msg.child_region[i].regionName + '</option>')
					
				}
		},
		error: function (XMLHttpRequest, textStatus, thrownError) {
		}
	
	});
	}

	loadAreas(choosedAreaId);
	loadSquares();

}

function squareChange(squareId){
	var squareId = $("#square").val().trim();

	$("#floor option:gt(0)").remove();
	if(squareId != undefined){

		$.ajax({
		url:"getFloors",
		type:"get",
		dataType:"json",
		async:false,
        data: { //发送给数据库的数据
			squareId:squareId
            },
		success:function(msg){
            	if(msg.child_region.length==0){
            		$("#squareId").append('<option value="999"' +'selected="true"'+  ' >暂无数据</option>');
            		return;
            	}
				for (var i=0; i<msg.child_region.length; i++){
					if(squareId == msg.child_region[i].regionId){
						$("#squareId").append('<option value="' + msg.child_region[i].regionId + '"' +'selected="true"'+  ' >' +msg.child_region[i].regionName + '</option>');
						continue;
					}
				}
				getFloors();
		},
		error: function (XMLHttpRequest, textStatus, thrownError) {
			alert("error");
		}
	
	});
	
	}
	
}

function loadAreas(choosedAreaId){

	$("#area option:gt(0)").remove()
	var cityId = $("#city").val().trim();
	if ($.trim(cityId) > 0)
	{
		$.ajax({
		url:"getAreas",
		type:"get",
		dataType:"json",
		async:false,
        data: { //发送给数据库的数据
            cityId:cityId
            },
		success:function(msg){
				for (var i=0; i<msg.child_region.length; i++){
					if(choosedAreaId == msg.child_region[i].regionId){
						$("#area").append('<option value="' + msg.child_region[i].regionId + '"' +'selected="true"'+  ' >' +msg.child_region[i].regionName + '</option>');
						continue;
					}
					$("#area").append('<option value="' +  msg.child_region[i].regionId + '">' +  msg.child_region[i].regionName + '</option>')
					
				}
		},
		error: function (XMLHttpRequest, textStatus, thrownError) {
		}
	
	});
	}
	loadSquares();
}

function getFloors(choosedFloorId){

	var squareId = $("#square").val().trim();
	if (squareId != undefined)
	{
		$.ajax({
		url:"getFloors",
		type:"get",
		dataType:"json",
		async:false,
        data: { //发送给数据库的数据
			squareId:squareId
            },
		success:function(msg){
            	if(squareId == "999"){
            		$("#floor option").remove()
            		$("#floor").append('<option value="999">暂无数据</option>');
            		return;
            	}else{
            		$("#floor option").remove();
            		$("#floor").append('<option value="-1">全部楼层</option>');
            	}
				for (var i=0; i<msg.child_region.length; i++){
					if(choosedFloorId == msg.child_region[i].regionId){
						$("#floor").append('<option value="' + msg.child_region[i].regionId + '"' +'selected="true"'+  ' >' +msg.child_region[i].regionName + '</option>');
						continue;
					}
					$("#floor").append('<option value="' +  msg.child_region[i].regionId + '">' +  msg.child_region[i].regionName + '</option>');
					
				}
		},
		error: function (XMLHttpRequest, textStatus, thrownError) {
		}
	
	});
	}
}


function loadSquares(){
	
    $("#square option").remove();
    var provinceId = $('#province').val().trim();
    var cityId = $('#city').val().trim();
    var areaId = $('#area').val().trim();
    
	$.ajax({
		url:"getSquares",
		type:"get",
		dataType:"json",
		async:false,
		data:{
			provinceId : provinceId,
			cityId : cityId,
			areaId : areaId
		},
		success:function(msg){
			if(msg.child_region.length == 0){
				$("#square").append('<option value="999">暂无数据</option>');
				getFloors();
				return;
			}
			for (var i=0; i<msg.child_region.length; i++){
				$("#square").append('<option value="' + msg.child_region[i].regionId + '">' + msg.child_region[i].regionName + '</option>');
			}
		},
		error: function (XMLHttpRequest, textStatus, thrownError) {
			
		}
	
	});
	
	squareChange();
}

function provinceChanged(){
	loadCitys();
}

function cityChanged(){
	loadAreas();
}


// 查询用户
function search() {
	showLoading();
	var currentPage = $('#gotoPage').val().trim();
	var provinceId = $('#province').val().trim();
	var cityId = $('#city').val().trim();
	var areaId = $('#area').val().trim();
	var square = $('#square').val().trim();
	var floor = $('#floor').val().trim();
	var siteName = $('#siteName').val().trim();
	$.ajax( {
		type : "GET",
		contentType : "application/json",
		data : {
			currentPage : currentPage,
			provinceId : provinceId,
			cityId : cityId,
			areaId : areaId,
			square : square,
			floor : floor,
			siteName : encodeURI(siteName)
		},
		dataType : 'json',
		url : "ajaxSiteList",
		async : true,
		success : function(msgs) {
			$('#leftFY').show();
			$('#rightFY').show();
			$('#myTable tr:not(:first)').empty();
			var siteList = msgs.siteList;
			$('#curpage').html(msgs.currentPage);
			$('#allpage').html(msgs.maxPage);
			if (msgs.maxPage == 1) {
				$('#leftFY').hide();
				$('#rightFY').hide();
			}
			if (msgs.currentPage == 1) {
				$('#leftFY').hide();
			}
			if (msgs.currentPage == msgs.maxPage) {
				$('#rightFY').hide();
			}
			if(siteList.length==0){
				$('#divfanye').hide();
			}else{
				$('#divfanye').show();
			}
			// 获取语言包
			var language = msgs.language;
			// 获取当前登录用户名
			var recentUserName = msgs.recentUserName;
			// 获取删除权限
			var isDelete = msgs.isDelete;
			if(siteList.length==0){
				$('#myTable')
				.append("<tr><td class='xytd' colspan=7>没有您要查询的数据！</tr>");
			}
			else{
			for ( var i = 0; i < siteList.length; i++) {
				var siteStatus = "";
				if(siteList[i].siteStatus == "1"){
					siteStatus = "运行";
				}else if(siteList[i].siteStaus == "2"){
					siteStatus = "暂停";
				}else{
					siteStatus = "关闭"
				}
				
				var view = " <a href='#' class='detail fontOrange' onmouseover='querySiteData(this);'><span>"+language.detail+"</span></a>";
				// 根据权限拼接按钮
				var anniu = "";
				// 如果有修改权限
				if(language.modify!=''){
					anniu+=" <a href='#' class='fontOrange' onclick='editSiteData(this);'><span>"+language.edit+"</span></a>";
				}
				// 如果有删除权限
				if(isDelete!=''){
				}
				$('#myTable')
						.append(
								"<tr>"
										+ "<td>"
										+ siteList[i].siteId
										+ "</td><td>"
										+ siteList[i].siteName
										+ "</td><td class='netArea'>"
										+ siteList[i].netArea
										+ "</td><td class='staffNo'>"
										+ siteList[i].staffNo
										+ "</td><td>"
										+ siteStatus
										+ "</td><td>"
										+ siteList[i].address
										+ "</td><td><div class='editdiv'>"
										+anniu+view
										+"</div></td></tr>");
			}}
			hideLoading();
		},
		error : function(mm, a, b, c, d, e) {
			hideLoading();
			myAlert("提示", "服务器好像开小差了，刷新一下试试吧！");
		}
	});
}


//分页栏
function gotopage(i) {
	showLoading();
	// 纯数字正则
	var numRule = /(^\d*$)|(^\S+\s+\S+$)/;
	var j = 1;
	var maxPage = parseInt($("#allpage").html());
	// 首页
	if (i == 0) {
		j = 1;
	}
	// 下一页
	else if (i == 1) {
		j = parseInt($("#curpage").html()) + 1;
	}
	// 上一页
	else if (i == 2) {
		j = parseInt($("#curpage").html()) - 1;
	}
	// 跳转
	else if (i == 3) {
		var gotoPage = $("#txtpages").val().trim();
		if (gotoPage == "") {
			j = 1;
		} else if (!gotoPage.match(numRule)) {
			j == 1
		} else {
			j = parseInt(gotoPage);
		}
	}
	// 尾页
	else if (i == -1) {
		j = maxPage;
	}
	if (j < 1) {
		j = 1;
	}
	if (j > maxPage) {
		j = maxPage;
	}
	$("#gotoPage").val(j);
	search();
}


